/**
 * @Title: UserController.java
 * @Package com.kaver.sso.web.controller.rest
 * @author roc
 * @date Jul 27, 2021 10:00:08 AM
 * @version V1.0
 * @copyright kaver
 */
package com.kaver.sso.web.controller.rest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.kaver.sso.def.BizException;
import com.kaver.sso.def.Result;
import com.kaver.sso.def.enums.CodeEnum;
import com.kaver.sso.def.req.AddUserReq;
import com.kaver.sso.def.req.CheckTicketReq;
import com.kaver.sso.def.req.GetTicketReq;
import com.kaver.sso.def.req.LockUserReq;
import com.kaver.sso.def.req.ResetPwdReq;
import com.kaver.sso.def.req.TokenInfoReq;
import com.kaver.sso.def.req.UpdatePwdReq;
import com.kaver.sso.def.req.UpdateUserReq;
import com.kaver.sso.def.req.UserPageListReq;
import com.kaver.sso.def.resp.PageResp;
import com.kaver.sso.def.resp.User;
import com.kaver.sso.helper.UnAuth;
import com.kaver.sso.service.UserService;

/**
 * @ClassName: UserController
 * @Description: rest接口
 * @author roc
 * @date Jul 27, 2021 10:00:08 AM
 * 
 */
@RestController
@RequestMapping("/api/user")
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    RedisTemplate<String, Object> redisTemplate;
    /**
     * 添加用户
     */
    @PostMapping("/addUser")
    public Result<Void> addUser(@RequestBody AddUserReq req) {
        userService.addUser(req);
        return Result.createSuccess();
    }
    /**
     * 修改用户
     */
    @PostMapping("/updateUser")
    public Result<Void> updateUser(@RequestBody UpdateUserReq req) {
        userService.updateUser(req);
        return Result.createSuccess();
    }
    /**
     * 重置用户密码
     */
    @PostMapping("/resetPwd")
    public Result<Void> resetPwd(@RequestBody ResetPwdReq req) {
        userService.resetPwd(req);
        return Result.createSuccess();
    }
    /**
     * 锁定用户
     */
    @PostMapping("/lockUser")
    public Result<Void> lockUser(@RequestBody LockUserReq req) {
    	userService.lockUser(req);
        return Result.createSuccess();
    }
    /**
     * 解锁用户
     */
    @PostMapping("/unlockUser")
    public Result<Void> unlockUser(@RequestBody LockUserReq req) {
    	userService.unlockUser(req);
        return Result.createSuccess();
    }
    
    /**
     * 用户分页查询
     */
    @PostMapping("/userPageList")
    public Result<PageResp<User>> userPageList(@RequestBody UserPageListReq req) {
        return Result.createSuccess(userService.userPageList(req));
    }
    
    /**
     * 获取ticket
     */
    @PostMapping("/getTicket")
    public Result<String> getTicket(@RequestBody GetTicketReq req) {
        User user = (User)redisTemplate.opsForValue().get(req.getToken());
        if (user == null) {
            throw new BizException(CodeEnum.F_000006);
        }
        return Result.createSuccess(userService.getTicket(user));
    }

    /**
     * 校验ticket 返回token(只能校验一次)
     */
    @UnAuth
    @PostMapping("/checkTicket")
    public Result<String> checkTicket(@RequestBody CheckTicketReq req) {
        if (!userService.checkApp(req.getAppId(), req.getAppSecret())) {
            throw new BizException(CodeEnum.F_000012);
        }
        String token = userService.ticketSwapToken(req.getTicket());
        if (token == null) {
            throw new BizException(CodeEnum.F_000007);
        }
        return Result.createSuccess(token);
    }

    /**
     * 获取token信息
     */
    @UnAuth
    @PostMapping("/tokenInfo")
    public Result<User> tokenInfo(@RequestBody TokenInfoReq req) {
        User user = (User)redisTemplate.opsForValue().get(req.getToken());
        if (user == null) {
            throw new BizException(CodeEnum.F_000006);
        }
        return Result.createSuccess(user);
    }
    /**
     * 修改密码
     * */
    @PostMapping("/updatePwd")
    public Result<Void> updatePwd(@RequestBody UpdatePwdReq req) {
        userService.updatePwd(req);
        return Result.createSuccess();
    }
    
}
